package arm;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

public class MainBean
{
    public boolean getAccessAdmin()
    {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof UserDetails) {
            String role = "";
            for(GrantedAuthority ga: ((UserDetails)principal).getAuthorities())
            {
                role = ga.getAuthority();
            }
            return role.equals("ROLE_ADMIN");
        } else {
            return false;
        }
    }

    public boolean getAccessUser()
    {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof UserDetails) {
            String role = "";
            for(GrantedAuthority ga: ((UserDetails)principal).getAuthorities())
            {
                role = ga.getAuthority();
            }
            return role.equals("ROLE_USER");
        } else {
            return false;
        }
    }
}
